/**
 * 轮询报到
 */
import axios from "@/plugin/axios";
export default {
  data() {
    return {
      interval: null
    };
  },
  beforeRouteLeave(to, from, next) {
    clearInterval(this.interval);
    this._leaveRoom();
    next();
  },
  methods: {
    _checkIn() {
      const data = { roomId: this.$route.params.id };
      if(!data.roomId) return;
      const options = { url: "chatroom/room/checkIn", method: "post", data };
      axios(options);
    },
    _leaveRoom() {
      const data = { roomId: this.$route.params.id };
      const options = { url: "chatroom/room/leave", data, method: "post" };
      axios(options).catch(() => {});
    }
  },
  created() {
    this.interval = setInterval(this._checkIn.bind(this), 30 * 1000); // 30秒报到一次
  }
};
